package com.example.lianjuan.ui.adapter;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextClock;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.lianjuan.Base.BaseApplication;
import com.example.lianjuan.Base.IBaseInfo;
import com.example.lianjuan.Model.domain.SelectContent;
import com.example.lianjuan.R;
import com.example.lianjuan.Utils.Constants;
import com.example.lianjuan.Utils.LogUtils;
import com.example.lianjuan.Utils.UtilsUrl;
import com.example.lianjuan.ui.activeity.TicketActivity;

import java.util.ArrayList;
import java.util.List;

public class PagerAdapter extends RecyclerView.Adapter<PagerAdapter.InnerHolder> {
    private List<SelectContent.DataDTO.TbkDgOptimusMaterialResponseDTO.ResultListDTO.MapDataDTO> data = new ArrayList<>();
    private OnClickItemListener onClickItemListener;

    @NonNull
    @Override
    public InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemItem = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_select_pager, parent, false);
        return new InnerHolder(itemItem);
    }

    @Override
    public void onBindViewHolder(@NonNull InnerHolder holder, int position) {
        holder.setData(position);
        //TODO 领卷按钮淘宝
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public void setData(SelectContent content) {
        if (content.getCode() == Constants.SUCCESS_CODE) {
            List<SelectContent.DataDTO.TbkDgOptimusMaterialResponseDTO.ResultListDTO.MapDataDTO> item = content.getData().getTbk_dg_optimus_material_response().getResult_list().getMap_data();
            data.clear();
            data.addAll(item);
            notifyDataSetChanged();
        }
    }

    public class InnerHolder extends RecyclerView.ViewHolder {
        private ImageView selectPicturePager;
        private TextView selectPreferential, selectContent, selectTicketShop, selectOriginalPrice;

        public InnerHolder(@NonNull View itemView) {
            super(itemView);
            selectPicturePager = itemView.findViewById(R.id.select_picture_pager);
            selectPreferential = itemView.findViewById(R.id.select_preferential);
            selectContent = itemView.findViewById(R.id.select_content);
            selectTicketShop = itemView.findViewById(R.id.select_ticket_shop);
            selectOriginalPrice = itemView.findViewById(R.id.select_original_price);
        }

        public void setData(int position) {
            //优惠价的价格
            Integer coupon_amount = data.get(position).getCoupon_amount();
            //原价格
            String reserve_price = data.get(position).getReserve_price();
            //最终价格转成浮点
            float priceFloat = Float.parseFloat(reserve_price);
            LogUtils.d(PagerAdapter.this, "coupon_amount-->" + coupon_amount + "，" +
                    "reserve_price-->" + reserve_price + ",");
            String pictUrl = UtilsUrl.getCoverPath(data.get(position).getPict_url(), 220);
            LogUtils.d(this, "pictUrl---->" + pictUrl);
            Glide.with(itemView.getContext()).load(pictUrl).into(selectPicturePager);
            //商品名称
            selectContent.setText(data.get(position).getTitle());
            //点击购买事件
            selectTicketShop.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    SelectContent.DataDTO.TbkDgOptimusMaterialResponseDTO.ResultListDTO.MapDataDTO mapData = data.get(position);
                    onClickItemListener.onClickItem(mapData);
                }
            });
            String url = data.get(position).getCoupon_click_url();
            //等于0时表示优惠价为0
            if (url == null) {
                //隐藏领卷按钮
                selectTicketShop.setVisibility(View.GONE);
                selectPreferential.setVisibility(View.GONE);
                selectOriginalPrice.setText(itemView.getResources().getString(R.string.on_preferential));
            } else {
                selectTicketShop.setVisibility(View.VISIBLE);
                selectPreferential.setVisibility(View.VISIBLE);
                //商品原价
                selectOriginalPrice.setText(String.format(itemView.getResources().getString(R.string.original_price), priceFloat));
                //优惠多少钱
                selectPreferential.setText(String.format(itemView.getResources().getString(R.string.coupon_how), coupon_amount));
            }
        }
    }

    public void setClickItemListener(OnClickItemListener onClickItemListener) {
        this.onClickItemListener = onClickItemListener;
    }

    public interface OnClickItemListener {
        void onClickItem(IBaseInfo iBaseInf);
    }
}
